package com.hz.userService.controller;

import com.hz.apiService.model.request.ApiResponse;
import com.hz.userService.domain.Class;
import com.hz.userService.domain.Grade;
import com.hz.userService.service.ClassService;
import com.hz.userService.service.GradeService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author LiiiYiAn
 * @version 1.0
 * @createDate 2023-09-24 11:31
 * @projectName SchoolServicePlatformFinal
 * @className ClassManagerController
 * @description 控制器 - 班级管理
 */
@RestController
@RequestMapping("/class-manager")
@RequiredArgsConstructor  // 使用构造器自动注入
public class ClassManagerController {
    private final GradeService gradeService;
    private final ClassService classService;

    @PostMapping("/add-grade")
    public String addGrade(@RequestParam("gradeName") String gradeName, @RequestParam("classNumber") Integer classNumber,
                           @RequestParam("gradeYear") String gradeYear) {
        Boolean add = gradeService.addGrade(gradeName, classNumber, gradeYear);
        return add ? ApiResponse.ok(null) : ApiResponse.intError();
    }

    @PostMapping("/edit-grade")
    public String editGrade(@RequestParam("id") Integer id, @RequestParam("gradeName") String gradeName,
                            @RequestParam("classNumber") Integer classNumber, @RequestParam("gradeYear") String gradeYear) {
        Boolean edit = gradeService.editGrade(id, gradeName, classNumber, gradeYear);
        return edit ? ApiResponse.ok(null) : ApiResponse.intError();
    }

    @PostMapping("/delete-grade")
    public String deleteGrade(@RequestParam("id") Integer id) {
        return gradeService.deleteGrade(id) ? ApiResponse.ok(null) : ApiResponse.intError();
    }

    @PostMapping("/view-grade")
    public String viewGrade(@RequestParam("id") Integer id) {
        Grade grade = gradeService.viewGrade(id);

        return grade != null ? ApiResponse.ok(grade) : ApiResponse.resNotFound();
    }

    @PostMapping("/add-class")
    public String addClass(@RequestParam("className") String className, @RequestParam("gradeId") Integer gradeId) {

        return classService.addClass(className, gradeId) ? ApiResponse.ok(null) : ApiResponse.intError();
    }

    @PostMapping("/edit-class")
    public String editClass(@RequestParam("id") Integer id, @RequestParam("className") String className,
                            @RequestParam("gradeId") Integer gradeId) {

        return classService.editClass(id, className, gradeId) ? ApiResponse.ok(null) : ApiResponse.intError();
    }

    @PostMapping("/delete-class")
    public String deleteClass(@RequestParam("id") Integer id) {

        return classService.deleteClass(id) ? ApiResponse.ok(null) : ApiResponse.resNotFound();
    }

    @PostMapping("/view-class")
    public String viewClass(@RequestParam("id") Integer id) {
        Class clas = classService.viewClass(id);

        return clas != null ? ApiResponse.ok(clas) : ApiResponse.resNotFound();
    }
}
